<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table
				:data="companyList.filter(data => !search || data.companyName.toLowerCase().includes(search.toLowerCase()))"
				v-loading="listLoading"
				border
				row-key="_id"
				:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
				@selection-change="selsChange" style="width: 100%;">
			<el-table-column prop="companyName" label="企业名称" min-width="120" sortable></el-table-column>
			<el-table-column prop="companyDes" label="企业介绍" min-width="120" sortable></el-table-column>
			<el-table-column label="操作" width="250">
				<template slot="header" slot-scope="scope">
					<el-input
							v-model="search"
							size="mini"
							placeholder="输入企业名字搜索"/>
				</template>
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<!--<el-col :span="24" class="toolbar">-->
			<!--<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>-->
			<!--<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">-->
			<!--</el-pagination>-->
		<!--</el-col>-->

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px"  ref="editForm">

				<el-form-item label="企业名称" prop="name">
					<el-input v-model="editForm.companyName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="企业介绍" prop="name">
					<el-input v-model="editForm.companyDes" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" ref="addForm">
				<el-form-item label="企业名称" prop="name">
					<el-input v-model="addForm.companyName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="企业介绍" prop="name">
					<el-input type="textarea" v-model="addForm.companyDes" auto-complete="off" :rows="2"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import {addCompany,editCompany,getCompanyList,delCompany} from "@/api/company/api";
	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				companyList: [],
				listLoading: false,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				//编辑界面数据
				editForm: {
					companyName: '',   //企业名字
					companyDes: '',  //企业介绍
					id: '', // id
				},
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				//新增界面数据
				addForm: {
					companyName: '',   //企业名字
					companyDes: '',  //企业介绍
				},
				search: ''
			}
		},
		methods: {
			searchChange(){
				this.companyList = this.companyList.filter(data => !this.search || data.companyName.toLowerCase().includes(this.search.toLowerCase()))
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getCompany();
			},
			//获取企业列表
			getCompany() {
				this.listLoading = true;
				getCompanyList().then((res) => {
					this.companyList = res.data;
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let para = { id: row._id };
					delCompany(para).then((res) => {
						this.listLoading = false;
						this.getCompany();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm.companyName = row.companyName;
				this.editForm.companyDes = row.companyDes;
				this.editForm.id = row._id;
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							let para = Object.assign({}, this.editForm);
							editCompany(para).then((res) => {
								if(res.code == 200) {
									this.editLoading = false;
									this.editFormVisible = false;
									this.getCompany();
								}
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = Object.assign({}, this.addForm);
							addCompany(para).then((res) => {
								if(res.code == 200){
									Object.keys(this.addForm).forEach(key => this.addForm[key] = '');
									this.addLoading = false;
									this.addFormVisible = false;
									this.getCompany()
								}
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
		},
		mounted() {
			this.getCompany();
		}
	}

</script>

<style scoped>

</style>
